package it.polimi.traveldream;

import java.util.ArrayList;

import it.polimi.traveldream.entities.Custompackage;
import it.polimi.traveldream.entities.Excursion;
import it.polimi.traveldream.entities.Hotel;
import it.polimi.traveldream.entities.PackageBase;
import it.polimi.traveldream.entities.Transport;
import it.polimi.traveldream.entities.User;






import javax.annotation.Resource;
import javax.ejb.EJBContext;
import javax.ejb.Stateful;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * Session Bean implementation class GestionePacchetto
 */
@Stateless
public class GestionePacchetto implements GestionePacchettoLocal {

	@PersistenceContext
	private EntityManager em;
	
	@Resource
	private EJBContext context;
	
	private Custompackage cstmpackage;
	
	
	@Override
	public void salva(int selectedHotel,int selectedvoloandata,int selectedvoloritorno,ArrayList<String> escur,int npersone,int id){
		
		
		ArrayList<Excursion>tmpEx=new ArrayList<Excursion>();
		CustompackageDTO customdto=new CustompackageDTO();
		PackageBase tmp=new PackageBase();
		
		tmp=em.find(PackageBase.class, id); // cerco tramite query PAckageBase scelto
				
		customdto.setNome(tmp.getNome());	
		customdto.setCitta(tmp.getCitta());
		customdto.setDescrizione(tmp.getDescrizione());
				
		Custompackage custom=new Custompackage(customdto);
		
		custom.setHotel(em.find(Hotel.class,selectedHotel)); /// trovo tramite query hotel scelto
		custom.setAndata(em.find(Transport.class, selectedvoloandata));
		custom.setRitorno(em.find(Transport.class, selectedvoloritorno));
		
		
		for(String i:escur)
			tmpEx.add(em.find(Excursion.class,Integer.parseInt(i)));	
		
		custom.setExcursions(tmpEx);
		custom.setPackagageBase(tmp);
		custom.setNPersone(npersone);
		custom.setPrezzo(calcolaCosto(custom)*npersone);
		
		
		custom.setUser(find(getPrincipalEmail()));
		this.cstmpackage=custom;
		System.out.print("Salvataggio custom");
		//em.persist(custom);
    }

	@Override
	public String salvaindb(){
		System.out.print("salvataggio nel db");
		em.persist(cstmpackage);
		return "/users/index.xhtml?faces-redirect=true";
	}
		
    private double calcolaCosto(Custompackage custom) {
		
		return custom.getAndata().getPrezzo()+custom.getRitorno().getPrezzo()+custom.getHotel().getPrice()+costoEscursioni(custom);
	}
    
    private double costoEscursioni(Custompackage custom) {
		
      double somma=0;
      for(Excursion e:custom.getExcursions()){
    	  somma+=e.getPrice();
      }
		return somma;
	}


	/*
	@Override
	public void update(UtenteDTO user) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void unregister() {
		// TODO Auto-generated method stub
		
	}

	
	@Override
	public UtenteDTO getUserDTO() {
		return null;
	}*/
	
	// --------------------------------------------------------------------------------------------------------------------
	
	
	
	


	private String getPrincipalEmail() {
		return context.getCallerPrincipal().getName();
    }
	
	 private User find(String email) {
	    	return em.find(User.class, email);
	     }


	public Custompackage getCstmpackage() {
		return cstmpackage;
	}


	public void setCstmpackage(Custompackage cstmpackage) {
		this.cstmpackage = cstmpackage;
	}

}
